import wx #the wx library is used to generate all GUI elements
import comms #this module lets us read comms status
import time

panelSpacing = 5 #number of pixels between various panes
panelSideBuffer = 15 #number of pixels between panels and edge of sidebar

class dispTextCtrl(wx.TextCtrl): #adds an auto-update timer to the TextControl class
    def __init__(self, parent, id, str):
        wx.TextCtrl.__init__(self, parent, id, str, style=wx.TE_READONLY)
        self.SetBackgroundColour('#FAFAFA')

class sidebar(wx.Panel):
    def __init__(self, parent, id=wx.ID_ANY):
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour('#EEEEEE')

        primarySizer = wx.BoxSizer(wx.VERTICAL)

        #plane feedback box
        planeInfo = wx.Panel(self, wx.ID_ANY)
        planeInfoSizer = wx.StaticBoxSizer(wx.StaticBox(planeInfo, wx.ID_ANY, 'Plane Information'), orient=wx.VERTICAL) #bounding box

        planeInfoGrid = wx.FlexGridSizer(6, 2, 5, 5) #grid containing other information
        latitude = wx.StaticText(planeInfo, wx.ID_ANY, 'Latitude:')
        latCoord = dispTextCtrl(planeInfo, wx.ID_ANY, str(comms.GetLat()) + ' deg')
        longitude = wx.StaticText(planeInfo, wx.ID_ANY, 'Longitude:')
        longCoord = dispTextCtrl(planeInfo, wx.ID_ANY, str(comms.GetLong()) + ' deg')
        altitude = wx.StaticText(planeInfo, wx.ID_ANY, 'Altitude:')
        altReading = dispTextCtrl(planeInfo, wx.ID_ANY, str(comms.GetAlt()) + 'm')
        groundSpeed = wx.StaticText(planeInfo, wx.ID_ANY, 'Ground Speed:')
        speedReading = dispTextCtrl(planeInfo, wx.ID_ANY, str(comms.GetSpeed()) + ' m/s')
        heading = wx.StaticText(planeInfo, wx.ID_ANY, 'Heading:')
        headingReading = dispTextCtrl(planeInfo, wx.ID_ANY, str(comms.GetHeading()) + ' deg')
        battVolt = wx.StaticText(planeInfo, wx.ID_ANY, 'Battery voltage:')
        voltage = dispTextCtrl(planeInfo, wx.ID_ANY, str(comms.GetVoltage())+ 'v')
        nextWaypoint = wx.StaticText(planeInfo, wx.ID_ANY, 'Next waypoint:')
        wayptNumber = dispTextCtrl(planeInfo, wx.ID_ANY, str(comms.GetNextWaypoint()))

        planeInfoGrid.AddMany([(latitude), (latCoord),
                               (longitude), (longCoord),
                               (altitude), (altReading),
                               (groundSpeed), (speedReading),
                               (heading), (headingReading),
                               (battVolt), (voltage),
                               (nextWaypoint), (wayptNumber)])
        planeInfoSizer.Add(planeInfoGrid, 0, wx.ALL, 5)
        planeInfo.SetSizer(planeInfoSizer)
        primarySizer.Add(planeInfo, 0, wx.LEFT | wx.RIGHT | wx.TOP | wx.EXPAND, panelSideBuffer)
        primarySizer.AddSpacer(panelSpacing)

        #Flight mode box
        flightMode = wx.Panel(self, wx.ID_ANY)
        flightModeSizer = wx.StaticBoxSizer(wx.StaticBox(flightMode, wx.ID_ANY, 'Flight Mode'), orient=wx.VERTICAL) #bounding box
        flightModeSizer.Add(wx.RadioButton(flightMode, wx.ID_ANY, 'Manual', style=wx.RB_GROUP), 0, wx.ALL, 5)
        flightModeSizer.Add(wx.RadioButton(flightMode, wx.ID_ANY, 'Waypoint'), 0, wx.BOTTOM | wx.LEFT, 5)
        flightModeSizer.Add(wx.RadioButton(flightMode, wx.ID_ANY, 'RTL'), 0, wx.BOTTOM | wx.LEFT, 5)

        flightMode.SetSizer(flightModeSizer)
        primarySizer.Add(flightMode, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, panelSideBuffer)
        primarySizer.AddSpacer(panelSpacing)

        #Camera settings
        cameraPanel = wx.Panel(self, wx.ID_ANY)
        cameraSizer = wx.StaticBoxSizer(wx.StaticBox(cameraPanel, wx.ID_ANY, 'Camera'), orient=wx.VERTICAL) #bounding box
        cameraSizer.Add(wx.Button(cameraPanel, wx.ID_ANY, 'Take picture'), 0, wx.ALL, 5)
        cameraCheckSizer = wx.BoxSizer(wx.HORIZONTAL) #Allows for the checkbox to be inset
        cameraCheckSizer.Add(wx.CheckBox(cameraPanel, wx.ID_ANY, 'Take pictures continuously'), 0, wx.LEFT | wx.RIGHT | wx.BOTTOM, 5)
        cameraSizer.Add(cameraCheckSizer, 0, wx.LEFT, 20)

        cameraPanel.SetSizer(cameraSizer)
        primarySizer.Add(cameraPanel, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, panelSideBuffer)
        primarySizer.AddSpacer(panelSpacing)

        #Loiter mode
        loiterPanel = wx.Panel(self, wx.ID_ANY)
        loiterSizer = wx.StaticBoxSizer(wx.StaticBox(loiterPanel, wx.ID_ANY, 'Loiter'), orient=wx.VERTICAL)
        loiterSizer.Add(wx.CheckBox(loiterPanel, wx.ID_ANY, 'Loiter'), 0, wx.ALL, 5)
        loiterGrid = wx.FlexGridSizer(2, 2, 5, 5)
        loiterLat = wx.StaticText(loiterPanel, wx.ID_ANY, 'Latitude:')
        loiterLatCoord = dispTextCtrl(loiterPanel, wx.ID_ANY, '')
        loiterLong = wx.StaticText(loiterPanel, wx.ID_ANY, 'Longitude:')
        loiterLongCoord = dispTextCtrl(loiterPanel, wx.ID_ANY, '')

        loiterGrid.AddMany([(loiterLat), (loiterLatCoord),
                           (loiterLong), (loiterLongCoord)])
        loiterSizer.Add(loiterGrid, 0, wx.ALL | wx.EXPAND, 5)
        loiterPanel.SetSizer(loiterSizer)
        primarySizer.Add(loiterPanel, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, panelSideBuffer)

        self.SetSizer(primarySizer)